package com.lachesis.molecule.mbg;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.InnerEnum;
import org.mybatis.generator.api.dom.java.JavaElement;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.util.StringUtility;

/** 
 * @ClassName: FileCommentGenerator 
 * @Description: MBG Plugin 文件注释生成器
 * @author shiwei.hu shiwei.hu@lachesis-mh.com
 * @date 2017年10月31日 下午7:50:55 
 *  
 */
public class FileCommentGenerator implements CommentGenerator
{
    private Properties properties;
    private boolean suppressDate;
    private boolean suppressAllComments;

    public FileCommentGenerator()
    {
        this.properties = new Properties();
        this.suppressDate = false;
        this.suppressAllComments = false;
    }

    public void addJavaFileComment(CompilationUnit compilationUnit)
    {
        compilationUnit.addFileCommentLine("/*");
        compilationUnit.addFileCommentLine(" * @ClassName " + compilationUnit.getType().getShortName());
        compilationUnit.addFileCommentLine(" * @Description ");
        compilationUnit.addFileCommentLine(" * @version 1.0");
        compilationUnit.addFileCommentLine(" * @Date " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        compilationUnit.addFileCommentLine(" */");

        if(compilationUnit.getType().getShortName().indexOf("Mapper") == -1) {

            FullyQualifiedJavaType type = new FullyQualifiedJavaType("io.swagger.annotations.ApiModelProperty");
            compilationUnit.addImportedType(type);
        }
    }

    public void addComment(XmlElement xmlElement)
    {
        if (this.suppressAllComments)
            return;
    }

    public void addRootComment(XmlElement rootElement)
    {
    }

    public void addConfigurationProperties(Properties properties)
    {
        this.properties.putAll(properties);

        this.suppressDate = StringUtility.isTrue(properties
                                                 .getProperty("suppressDate"));

        this.suppressAllComments = StringUtility.isTrue(properties
                                                        .getProperty("suppressAllComments"));
    }

    protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete)
    {
        javaElement.addJavaDocLine(" *");
        StringBuilder sb = new StringBuilder();
        sb.append(" * ");
        sb.append("@mbggenerated");
        if (markAsDoNotDelete) {
            sb.append(" do_not_delete_during_merge");
        }
        String s = getDateString();
        if (s != null) {
            sb.append(' ');
            sb.append(s);
        }
        javaElement.addJavaDocLine(sb.toString());
    }

    protected String getDateString()
    {
        if (this.suppressDate) {
            return null;
        }
        return new Date().toString();
    }

    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable)
    {
        if (this.suppressAllComments) {
            return;
        }

        innerClass.addJavaDocLine("/**");
        innerClass.addJavaDocLine(" * @Description ");
        innerClass.addJavaDocLine(" * @version 1.0");
        innerClass.addJavaDocLine(" * @Date " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        innerClass.addJavaDocLine(" */");
    }

    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable)
    {
        if (this.suppressAllComments) {
            return;
        }

        StringBuilder sb = new StringBuilder();

        innerEnum.addJavaDocLine("/**");
        innerEnum
        .addJavaDocLine(" * This enum was generated by MyBatis Generator.");

        sb.append(" * This enum corresponds to the database table ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        innerEnum.addJavaDocLine(sb.toString());

        addJavadocTag(innerEnum, false);

        innerEnum.addJavaDocLine(" */");
    }

    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn)
    {
        if (this.suppressAllComments) {
            return;
        }

        String name = introspectedColumn.getActualColumnName();
        String value = introspectedColumn.getRemarks();		

        if(field.getType().equals(FullyQualifiedJavaType.getDateInstance())) {

            field.addJavaDocLine("@ApiModelProperty(name=\"" + name + "\", " + "value=\"" + value  + "\", example=\"2017-01-16T15:30:19.000+0800\")");

        } else {		

            field.addJavaDocLine("@ApiModelProperty(name=\"" + name + "\", " + "value=\"" + value + "\")");
        }


    }

    public void addFieldComment(Field field, IntrospectedTable introspectedTable)
    {
        if (this.suppressAllComments)
            return;
    }

    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable)
    {
        if (this.suppressAllComments)
            return;
    }

    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn)
    {
        if (this.suppressAllComments)
            return;
    }

    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn)
    {
        if (this.suppressAllComments)
            return;
    }

    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete)
    {
        innerClass.addJavaDocLine("/**");
        innerClass.addJavaDocLine(" * @Description ");
        innerClass.addJavaDocLine(" * @version 1.0");
        innerClass.addJavaDocLine(" * @Date " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        innerClass.addJavaDocLine(" */");
    }
}